<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\DB;

class WorldMapControler extends Controller
{
    //城市列表
    public function city_lists(){
    	$map_lists = Db::table('world_map')->where('is_display','=','1')->paginate(15);

    	return view('world_map_lists',['map'=>$map_lists]);
    }

    //添加城市
    public function city_add(){

    	return view('world_map_add');
    }

    //编辑城市
    public function city_edit(Request $request, $id){
        $where[] = ['id','=',$id];
        $where[] = ['is_display','=','1'];
    	$map_lists = Db::table('world_map')->where($where)->first();


    	return view('world_map_edit',['map_info'=>$map_lists]);
    }

    //添加城市动作
    public function city_add_do(Request $request){
    	$input = $request->input();

    	if(empty($input['id'])){
    		Db::table('world_map')->insert($input);
    	}else{
    		Db::table('world_map')->where('id','=',$input['id'])->update($input);
    	}

    	return response()->json([
            'status' => 1,
            'result' => [],
            'message' => '操作成功！'
        ]);
    }

    //删除城市
    public function city_del(Request $request, $id){

        Db::table('world_map')->where('id','=',$id)->update(['is_display'=>'-1']);
        Db::table('world_map_detail')->where('map_id','=',$id)->orWhere('map_guide','=',$id)->update(['is_display'=>'-1']);

        return response()->json([
            'status' => 1,
            'result' => [],
            'message' => '操作成功！'
        ]);
    }

    //跳转列表
    public function city_lists_info(Request $request, $id){
        //首发城市
        $where[] = ['map_id','=',$id];
        $where[] = ['is_display','=','1'];
        $map_lists = Db::table('world_map_detail')->where($where)->paginate(15);

        foreach ($map_lists as $key => $value) {
            $where = [];
            $where[] = ['id','=',$value->map_guide];
            $first_name = Db::table('world_map')->where($where)->first();

            $value->city_name = $first_name->city;
        }

        $guide_name = Db::table('world_map')->where('id','=',$id)->first();

        return view('world_map_guide',['map'=>$map_lists,'guide_name'=>$guide_name,'city_id'=>$id]);
    }

    //添加引导添加
    public function city_guide_add(Request $request, $city_id){
        $map_lists = Db::table('world_map')->where('is_display','=','1')->get();

        return view('world_map_guide_add',['city_id'=>$city_id,'map_lists'=>$map_lists]);
    }

    //编辑引导城市
    public function city_guide_edit(Request $request, $id, $city_id){
        $map_lists = Db::table('world_map')->where('is_display','=','1')->get();

        $map_detail = Db::table('world_map_detail')->where('id','=',$id)->first();

        return view('world_map_guide_edit',['city_id'=>$city_id, 'map_lists'=>$map_lists, 'map_detail'=>$map_detail,'id'=>$id]);
    }

    //添加引导添加动作
    public function city_guide_add_do(Request $request){
        $input = $request->input();

        if(empty($input['id'])){
            Db::table('world_map_detail')->insert($input);
        }else{
            Db::table('world_map_detail')->where('id','=',$input['id'])->update($input);
        }

        return response()->json([
            'status' => 1,
            'result' => [],
            'message' => '操作成功！'
        ]);
    }

    //删除城市引导
    public function city_guide_del(Request $request,$id){
        $where[] = ['id','=',$id];
        Db::table('world_map_detail')->where($where)->update(['is_display'=>'-1']);

        return response()->json([
            'status' => 1,
            'result' => [],
            'message' => '操作成功！'
        ]);

    }

}
